﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Operation class for getting multiple counts. The operation
    /// calls KeyspaceService.MultigetCount(), which gets the actual
    /// counts.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    public class MultigetCountOperation<K> : KeyspaceOperationCallback<Dictionary<K,int>>
    {
        public MultigetCountOperation() : base()
        {
        }
        public MultigetCountOperation(Serializer<K> keyserializer, string columnfamily) : base()
        {
            KeySerializer = keyserializer;
            ColumnFamily = columnfamily;
        }
        public MultigetCountOperation(Serializer<K> keyserializer, string columnfamily, HSlicePredicate<int> slice)
            : base()
        {
            KeySerializer = keyserializer;
            ColumnFamily = columnfamily;
            SlicePredicate = slice;
        }
        public Serializer<K> KeySerializer
        {
            get;
            set;
        }
        public string ColumnFamily
        {
            get;
            set;
        }
        public HSlicePredicate<int> SlicePredicate
        {
            get;
            set;
        }
        public List<K> Keys
        {
            get;
            set;
        }
        public void SetKeySerializer(Serializer<K> ser)
        {
            KeySerializer = ser;
        }
        public void SetSlicePredicate(HSlicePredicate<int> slicepred)
        {
            SlicePredicate = slicepred;
        }
        public override Dictionary<K, int> DoInKeyspace(KeyspaceService ks)
        {
            ColumnParent columnParent = new ColumnParent(ColumnFamily);
            Dictionary<K, int> counts = KeySerializer.FromBytesDictionary<int>(
                ks.MultigetCount(KeySerializer.ToBytesList(Keys), columnParent, SlicePredicate.ToThrift()));
            return counts;
        }
    }
}
